
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingDeque;


public class Maschen {
	private CopyOnWriteArrayList<Mash> maschen;

	public Maschen (Graph g){
		ConcurrentHashMap<Integer , Mash> init= new ConcurrentHashMap<Integer, Mash>();

		for(Knoten k :g.getKnmap().values()){
			int id =k.getCapacity();
			Mash actMash;
			if(init.containsKey(id)){
				actMash =init.get(id);	
			}else{
				actMash =new Mash(id);
				init.put(id, actMash);
			}
			init.get(id).add(k);

		}
		maschen = new CopyOnWriteArrayList<Mash>(init.values());
	}

	public synchronized void add(Mash neu){
		if(!containsMash(neu)){
		maschen.add(neu);
		}
	}

	public boolean containsMash(Mash m){
		return maschen.contains(m);
	}
	
	public void checkMash(Mash m){
		if (m.isempty()){
			maschen.remove(m);
		}
	}
	
	public void delEmpty(){
		for(Mash m : maschen){
			checkMash(m);
		}
	}
	
	public synchronized ConcurrentLinkedQueue<Mash> getMaschen() {
		return new ConcurrentLinkedQueue<Mash>(maschen);
	}

	public synchronized String toString(){
		StringBuffer sb = new StringBuffer();
		for (Mash m : maschen){
			sb.append(" {{"+ m +"}} " );

		}
		return sb.toString();
	}

}
